/******************** 
	作用:粉丝代理
	作者:蔡俊雄
	版本:V1.0
	时间:2015-07-27
********************/

$(function() {
	var tableId = "#dynamic-table"; //表格ID
	var treeUrl = "/Agent/Team?act=tree"; //团队树的网址
	var upgradeToAgentUrl = "/Agent/Fans?act=upgradeToAgent"; //升级代理的网址
	var manageUrl = "/Agent/Fans/update"; //管理的网址
	var getAppsUrl = "/app/getList"; //获取应用列表
	var replyUrl = "/Message/Reply"; //回复页面地址
	var superPartnerUrl = "/UserCenter/Fans?act=upgradeToSuper";



	General.initCheckbox(); //初始化表格中的复选框事件监听
	General.extendDialog(); //扩展对话框
	General.initAutoPostSelect(); //初始化页面中所有自动跳转的下拉列表


	//---------------------超级合伙人
	$('.super_partner').on("click",function(){		
		var tips = $(this).text();
		var is_subscribe = $(this).attr("data-subscribe");
		if(is_subscribe == 0) {
			alert("你是游客，不能成为超级合伙人");
			return;
		}
		if (confirm("您确定"+tips+'?')) {
			var is_super_partner=$(this).attr("data-super");
			var uid = $(this).closest("tr").attr("data-id");
			superPartner(is_super_partner,uid); 
		}
	});
	
	function superPartner(is_super_partner,uid) {
		var options = {};
		var url = superPartnerUrl;
				
		var data = {
				"is_super_partner":is_super_partner,
				"uid":uid,
				'update': 'update'
		};
		var tip = "正在设置";
		Common.ajaxPost(options, berforSuperPartner, url, data, tip, completeSuperPartner, failSuperPartner);
	};
	function berforSuperPartner(){}
	
	function completeSuperPartner(options, data, status) {
			data=$.parseJSON(data);
			if(data.success){
				alert("设置成功!");
			}
		General.hideMask();
		General.refresh(); //刷新页面
	};
	
	function failSuperPartner() {	
		alert("设置失败!");
	};

	//-----------------发送消息-----------------
	//点击"发送消息"按钮
	$(tableId + ">tbody a[data-id=reply]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var openid = $(this).closest("tr").attr("data-openid");//获取粉丝id
		var title="快速回复";
		showReplyDialog(title,openid);
	});
	
	/**
	 * 显示"发送消息"窗口
	 * @param {String} title 弹出窗口的标题
	 * @param {String} id 粉丝id
	 */
	function showReplyDialog(title,openid){
		var url = Common.addUrlParameters(replyUrl, {"id": openid});
//		General.showDialog(title,url);
		General.showDialogWidthHeight(title,url,500,420);
//		General.showDialogWidthHeight(title,url,500,340);
	}

	//-----------------添加到分组-----------------
	//点击"添加到分组"按钮
	/*$("#sureAdd").on("click", function(e) {
		//判断是否有行被选中
		var selectedRows = $(tableId+' > tbody input[type=checkbox]:checked');
		if (selectedRows.length > 0) {
			//判断是否选中了分组
			if($("#addToGroup").val()=="0"){
				General.alert("请先选中要添加到的分组");
				return false;
			}
			return true;
		} else {
			General.alert("请先选中要添加到分组的粉丝再进行操作!");
			return false;
		}
	});*/
	
	//-----------------查看团队树-----------------
	//点击"团队树"按钮
	$(tableId + ">tbody a[data-id=team]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var tr=$(this).closest("tr");
		var id = tr.attr("data-id");//获取粉丝id
//		var biz_id = tr.attr("data-biz_id");//获取biz_id
//		var public_id = tr.attr("data-public_id");//获取public_id
		var title="团队树";
		showTreeDialog(title,id);
	});
	
	//显示"团队树"窗口
	function showTreeDialog(title,id){
		var url = Common.addUrlParameters(treeUrl, {"id": id});
		General.showDialogWidthHeight(title,url,500,0);
	}
	
	//-----------------管理-----------------
	//点击"管理"按钮
	$(tableId + ">tbody a[data-id=manage]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var tr=$(this).closest("tr");
		var id = tr.attr("data-id");//获取粉丝id
//		var biz_id = tr.attr("data-biz_id");//获取biz_id
//		var public_id = tr.attr("data-public_id");//获取public_id
		var title="管理";
		showManageDialog(title,id);
	});
	
	//显示"管理"窗口
	function showManageDialog(title,id){
		var url = Common.addUrlParameters(manageUrl, {"uid": id});
		var maxWidth=700;
		var maxHeight=580;
		var width = $(window).width(); //获取窗口的宽度
		width = width * 0.9;
		if(width>maxHeight){
			width=maxWidth;
		}else{
			width="90%";
		}
		
		var height = $(window).height(); //获取窗口的高度
		height = height * 0.9;
		if(height>maxHeight){
			height=maxHeight;
		}else{
			height=0;
		}
		General.showDialogWidthHeight(title,url,width,height);
	}
	
	//-----------------升级V客-----------------
	//点击"升级V客"按钮
	$(tableId + ">tbody a[data-id=vke]:not([disabled])").on("click", function(e) {
		if (confirm("确定要升级为V客吗?")) {
			return true;
		}else{
			General.stopEvent(e);
		}
	});
	
	//-----------------升级代理-----------------
	//点击"升级代理"按钮
	$(tableId + ">tbody a[data-id=agent]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var id = $(this).closest("tr").attr("data-id");//获取粉丝id
		var title="升级代理";
		showAgentDialog(title,id);
	});
	
	//显示"升级代理"窗口
	function showAgentDialog(title,id){
		var url = Common.addUrlParameters(upgradeToAgentUrl, {"id": id});
		var height = $(window).height(); //获取窗口的高度
		height = height * 0.9;
		var maxHeight=670;
		if(height>maxHeight){
			height=maxHeight;
		}else{
			height=0;
		}
		General.showDialogWidthHeight(title,url,500,height);
	}
	
	//-----------------模拟登陆-----------------
	//点击"模拟登陆"按钮
	$(tableId + ">tbody a[data-id=simulateLogin]:not([disabled])").on("click", function(e) {
		General.stopEvent(e);
		var uid = $(this).closest("tr").attr("data-authid");//获取粉丝id
		var title="选择应用";
		showSimulateLoginDialog(title,uid);
	});
	
	//显示"模拟登陆"窗口
	function showSimulateLoginDialog(title,uid){
		var url = Common.addUrlParameters(getAppsUrl, {"uid": uid});
		var height = $(window).height(); //获取窗口的高度
		General.showDialogWidthHeight(title,url,500,height);
	}
});